package com.aranandroid.customview.squareview.base

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import androidx.annotation.Nullable
import com.aranandroid.customview.R
import com.aranandroid.customview.utils.DrawableBuilder

open class SquareFocusedView(
    @Nullable context: Context?,
    @Nullable attrs: AttributeSet?,
    squareNormalView: SquareNormalView
) {
    private var corner: Float = 0F

    var cornerlt: Float
    var cornerlb: Float
    var cornerrt: Float
    var cornerrb: Float

    var dashwidth: Float = 0F
    var dashgap: Float = 0F
    var linecolor: Int = 0
    var fillcolor: Int = 0
    var linewidth: Float = 0F

    var textColor: Int? = null

    // 图片
    var topDraw:Drawable? = null
    var leftDraw:Drawable? = null
    var bottomDraw:Drawable? = null
    var rightDraw:Drawable? = null


    fun setCorner(value:Float) {
        corner = value
        cornerlt = 0F
        cornerlb = 0F
        cornerrt = 0F
        cornerrb = 0F
    }



    init {
        val obtainStyledAttributes =
            context!!.obtainStyledAttributes(attrs, R.styleable.View)
        //圆角
        corner =
            obtainStyledAttributes.getDimension(R.styleable.View_focused_corner, squareNormalView.getCorner())
        cornerlt =
            obtainStyledAttributes.getDimension(R.styleable.View_focused_corner_left_top, squareNormalView.cornerlt)
        cornerlb = obtainStyledAttributes.getDimension(
            R.styleable.View_focused_corner_left_bottom,
            squareNormalView.cornerlb
        )
        cornerrt = obtainStyledAttributes.getDimension(
            R.styleable.View_focused_corner_right_top,
            squareNormalView.cornerrt
        )
        cornerrb = obtainStyledAttributes.getDimension(
            R.styleable.View_focused_corner_right_bottom,
            squareNormalView.cornerrb
        )
        //线宽
        linewidth =
            obtainStyledAttributes.getDimension(R.styleable.View_focused_line_width, squareNormalView.linewidth)
        //虚线宽度
        dashwidth =
            obtainStyledAttributes.getDimension(R.styleable.View_focused_dash_width, squareNormalView.dashwidth)
        // 虚线间距
        dashgap = obtainStyledAttributes.getDimension(R.styleable.View_focused_dash_gap, squareNormalView.dashgap)
        // 线颜色
        linecolor = obtainStyledAttributes.getColor(
            R.styleable.View_focused_line_color,
            squareNormalView.linecolor
        )
        // 填充颜色
        fillcolor = obtainStyledAttributes.getColor(
            R.styleable.View_focused_background_color,
            squareNormalView.fillcolor
        )
        textColor = obtainStyledAttributes.getColor(
            R.styleable.View_focused_text_color,
            squareNormalView.textColor
        )
        // draw 图片
        topDraw = obtainStyledAttributes.getDrawable(
            R.styleable.View_focused_drawable_top)
        topDraw?:let {
            topDraw = squareNormalView.topDraw
        }
        leftDraw = obtainStyledAttributes.getDrawable(
            R.styleable.View_focused_drawable_left)
        leftDraw?:let {
            leftDraw = squareNormalView.leftDraw
        }
        rightDraw = obtainStyledAttributes.getDrawable(
            R.styleable.View_focused_drawable_right)
        rightDraw?:let {
            rightDraw = squareNormalView.rightDraw
        }
        bottomDraw = obtainStyledAttributes.getDrawable(
            R.styleable.View_focused_drawable_bottom)
        bottomDraw?:let {
            bottomDraw = squareNormalView.bottomDraw
        }
    }


    fun getBackGround(): Drawable {
        var drawableBuilder = DrawableBuilder().corner()
        if (SquareNormalView.DEFZERO != corner) {
            drawableBuilder = drawableBuilder.corner(
                floatArrayOf(
                    corner, corner, corner, corner,
                    corner, corner, corner, corner
                )
            )
        }

        if (SquareNormalView.DEFZERO != cornerlt || SquareNormalView.DEFZERO != cornerlb || SquareNormalView.DEFZERO != cornerrt || SquareNormalView.DEFZERO != cornerrb) {
            drawableBuilder =
                drawableBuilder.corner(
                    floatArrayOf(
                        cornerlt, cornerlt, cornerrt, cornerrt,
                         cornerrb, cornerrb,cornerlb, cornerlb
                    )
                )
        }

        if (SquareNormalView.DEFZERO != linewidth) {
            drawableBuilder = drawableBuilder.line().lineWidth(linewidth).lineColor(linecolor)
        }

        if (SquareNormalView.DEFZERO != dashwidth) {
            drawableBuilder = drawableBuilder.dash().dashWidth(dashwidth).dashGap(dashgap)
        }

        if (fillcolor != 0) {
            drawableBuilder = drawableBuilder.fill(fillcolor)
        }
        return drawableBuilder.build()
    }

}